package com.example.makerspace.controller.socket;



import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.google.common.collect.Lists;
import com.dtflys.forest.annotation.Get;
import com.example.makerspace.dao.DemandDAO;
import com.example.makerspace.dao.ProductScoreDAO;
import com.example.makerspace.dao.UserDAO;
import com.example.makerspace.domain.DemandDO;
import com.example.makerspace.domain.EnterpriseDataDO;
import com.example.makerspace.domain.ProductScoreDO;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.Result;
import com.example.makerspace.excel.ExcelFormat;
import com.example.makerspace.excel.ExcelHeaderInfo;
import com.example.makerspace.forest.MyClient;
import com.example.makerspace.mapper.EnterpriseDataMapper;
import com.example.makerspace.utils.ExcelUtils;
import com.example.makerspace.vo.IndexOrNameDataListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 首页控制器
 * @author pan_junbiao
 **/
@Controller
@ResponseBody
public class IndexController {

    @Resource
    private DemandDAO demandDAO;

    @Resource
    private ProductScoreDAO productScoreDAO;

    @Resource
    private  MyClient myClient;


    @Resource
    private EnterpriseDataMapper enterpriseDataMapper;


    @Resource
    private UserDAO userDAO;
    @RequestMapping("/test3")
    public String test3()
    {
        List<UserDO> userDOS = userDAO.selectAll();
        for(int i=0;i<userDOS.size();i++){
            String newPass = DigestUtil.sha256Hex(userDOS.get(i).getPassword());
            userDOS.get(i).setPassword(newPass);
            userDAO.updateById(userDOS.get(i));
        }
        return "index";
    }


    @RequestMapping("/test")
    public  void test(String  url){
        String filePath = "/static/data"+url;

        // 将上传的 Excel 文件读取为对象列表
        // 这里默认读取第一个sheet
        EasyExcel.read(filePath, EnterpriseDataDO.class, new IndexOrNameDataListener(enterpriseDataMapper)).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
    }
    @RequestMapping("/test1")
    public Result test1() {
        // 调用自定义的 Forest 接口方法
        // 等价于发送 HTTP 请求，请求地址和参数即为 helloForest 方法上注解所标识的内容
        String s = myClient.simpleRequest();
        // result 即为 HTTP 请求响应后返回的字符串类型数据
        return  Result.success(s);
    }

    @GetMapping("/make")
    public  List<UserDO>  helloForest() {
        List<UserDO> userDOS = userDAO.selectAll();
        return  userDOS;
    }

    @RequestMapping("/docker")
    public String test2() {
        List<DemandDO> demandDOS = demandDAO.selectAll();
        List<DemandDO> collect = demandDOS.stream().filter(item -> item.getIsShow().equals(1)).collect(Collectors.toList());
        String username="13940001294";
        for(int i=0;i<collect.size();i++){
            ProductScoreDO productScoreDO = new ProductScoreDO();
            productScoreDO.setIsScore(0);
            productScoreDO.setProductId(collect.get(i).getId());
            productScoreDO.setUsername(username);
            productScoreDAO.insert(productScoreDO);
        }
        return null;
    }


    /**
     * 模拟数据库查询返回用户信息
     */
    public  List<UserDO> findUser() {
        List<UserDO> list = Lists.newArrayList();
        UserDO user = new UserDO();
        user.setId(10);
        user.setUsername("张三");
        user.setCreateTime(new Date());
        list.add(user);
        UserDO user1 = new UserDO();
        user1.setId(26);
        user1.setUsername("李四");
        user1.setCreateTime(new Date());
        list.add(user1);
        UserDO user2 = new UserDO();
        user2.setId(28);
        user2.setUsername("王丽");
        user2.setCreateTime(new Date());
        list.add(user2);
        return list;
    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response) {
        String fileName="产品列表";
        // 待导出数据
        List<DemandDO> demandDOS = demandDAO.selectAll();
        ExcelUtils excelUtils = new ExcelUtils(demandDOS, getHeaderInfo(), getFormatInfo());
        excelUtils.sendHttpResponse(response, fileName, excelUtils.getWorkbook());
    }
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(1, 1, 0, 0, "id"),

                new ExcelHeaderInfo(0, 0, 1, 3, "接口信息"),
                new ExcelHeaderInfo(1, 1, 1, 1, "接口名称"),
                new ExcelHeaderInfo(1, 1, 2, 2, "接口地址"),
                new ExcelHeaderInfo(1, 1, 3, 3, "接口执行时间"),

                new ExcelHeaderInfo(0, 0, 4, 7, "访问信息"),
                new ExcelHeaderInfo(1, 1, 4, 4, "访问人IP"),
                new ExcelHeaderInfo(1, 1, 5, 5, "访问人ID"),
                new ExcelHeaderInfo(1, 1, 6, 6, "状态"),
                new ExcelHeaderInfo(1, 1, 7, 7, "访问人名字"),

                new ExcelHeaderInfo(1, 1, 8, 8, "创建时间"),
                new ExcelHeaderInfo(1, 1, 9, 9, "修改时间")
        );
    }

    /**
     * 获取格式化信息
     *
     * @param :
     * @return: java.util.Map<java.lang.String,com.zhengqing.demo.modules.system.enumeration.ExcelFormat>
     */
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
        format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("userId", ExcelFormat.FORMAT_INTEGER);
        format.put("status", ExcelFormat.FORMAT_INTEGER);
        return format;
    }
}
